<?php if (!defined('ITPK')) exit('You can not directly access the file.');

/**
 * 机器人助手
 * @author 冬天的秘密
 * @link http://bbs.itpk.cn
 * @version 1.0
 */

class WebrobotaideHandler extends AbstractpluginHandler {

	/**
	 * 插件入口方法，所有插件都必须有main方法和继承AbstractpluginHandler类
	 */
	public function main() {
		$this->updatePluginId();
		$msg = (DataUtil::is_empty($this->msg) || DataUtil::is_contain("[faceArrayend]", $this->msg)) ? "[img:no]" : $this->msg;
		if ($this->is_permissions() || $this->is_manager()) {
			if ($msg == "更新群列表") {
				$result = false;
				$replace_values = array();
				$groupListObj = WqCommon::getGroupList($this->robot['bkn'], $this->cookie);
				if ($groupListObj) {
					$replaceSql = "REPLACE INTO web_group_info(uin, name, owner) VALUES";
					if (isset($groupListObj->create)) {
						$createGroupList = $groupListObj->create;
						for ($k = 0; $k < count($createGroupList); $k++) {
							array_push($replace_values, "(" . $createGroupList[$k]->gc . ", '" . addslashes($createGroupList[$k]->gn) . "', " . $createGroupList[$k]->owner . ")");
						}
					}
					if (isset($groupListObj->join)) {
						$joinGroupList = $groupListObj->join;
						for ($k = 0; $k < count($joinGroupList); $k++) {
							array_push($replace_values, "(" . $joinGroupList[$k]->gc . ", '" . addslashes($joinGroupList[$k]->gn) . "', " . $joinGroupList[$k]->owner . ")");
						}
					}
					if (isset($groupListObj->manage)) {
						$managerGroupList = $groupListObj->manage;
						for ($k = 0; $k < count($managerGroupList); $k++) {
							array_push($replace_values, "(" . $managerGroupList[$k]->gc . ", '" . addslashes($managerGroupList[$k]->gn) . "', " . $managerGroupList[$k]->owner . ")");
						}
					}
					$replace_values_str = implode(",", $replace_values);
					if (!DataUtil::is_empty($replace_values_str)) {
						$result = $this->db->executeQuery($replaceSql . $replace_values_str);
					}
				}
				return ReplyUtil::getReply($result ? ("群列表更新成功，本次共更新了" . count($replace_values) . "个群的资料") : "群列表更新失败");
			} elseif ($msg == "更新群成员") {
				$result = false;
				$start = 0;
				$end = 20;
				while (true) {
					$searchGroupMembers = WqCommon::searchGroupMembers($this->object['group_uin'], $start, $end, 0, $this->robot['bkn'], $this->cookie);
					if ($searchGroupMembers && $searchGroupMembers['ec'] == 0) {
						$adm_max = $searchGroupMembers['adm_max'];
						$adm_num = $searchGroupMembers['adm_num'];
						$count = $searchGroupMembers['count'];
						$max_count = $searchGroupMembers['max_count'];
						$levelname = $searchGroupMembers['levelname'];
						$mems = $searchGroupMembers['mems'];
						if ($start == 0) {
							$levelArray = array();
							for ($k = 1; ;$k++) {
								if (!isset($levelname["$k"])) {
									break;
								}
								array_push($levelArray, $levelname["$k"]);
							}
							$levelstr = implode(",", $levelArray);
							$levelstr = DataUtil::is_empty($levelstr) ? "潜水,冒泡,吐槽,活跃,唠叨,传说" : $levelstr;
							$updateSql = new UpdateSql("web_group_info");
							$updateSql->setUpdateValue("adm_max", $adm_max);
							$updateSql->setUpdateValue("adm_num", $adm_num);
							$updateSql->setUpdateValue("count", $count);
							$updateSql->setUpdateValue("max_count", $max_count);
							$updateSql->setUpdateValue("level", $levelstr);
							$updateSql->setWhere("uin", $this->object['group_uin']);
							$updateSql->executeUpdateSql($this->db);
						}
						$result = $this->groupmemberinfoPer->replaceMe($this->object['group_uin'], $mems);
						if (!$result || $end + 1 >= $count) {
							break;
						}
						$start = $end + 1;
						$end = $start + 20;
					} else {
						break;
					}
				}
				$tip = "";
				if ($result || $start > 0) {
					$upd_count = ($end == 20 || $end + 1 > $count) ? $count : $end + 1;
					$tip = "群成员更新成功，本次共更新了" . $upd_count . "个人的资料";
				} else {
					$tip = "群成员更新失败";
				}
				return ReplyUtil::getReply($tip);
			} elseif (DataUtil::start_contain("更新群成员", $msg)) {
				$result = false;
				$profile = explode("更新群成员", $msg);
				$search = trim($profile[1]);
				if ($search != "") {
					$start = 0;
					$end = 20;
					while (true) {
						$searchGroupMembers = WqCommon::searchGroupMembers($this->object['group_uin'], $start, $end, 0, $this->robot['bkn'], $this->cookie, $search);
						if ($searchGroupMembers && $searchGroupMembers['ec'] == 0) {
							$adm_max = $searchGroupMembers['adm_max'];
							$adm_num = $searchGroupMembers['adm_num'];
							$count = $searchGroupMembers['count'];
							$max_count = $searchGroupMembers['max_count'];
							$search_count = $searchGroupMembers['search_count'];
							$levelname = $searchGroupMembers['levelname'];
							$mems = $searchGroupMembers['mems'];
							if ($start == 0) {
								$levelArray = array();
								for ($k = 1; ;$k++) {
									if (!isset($levelname["$k"])) {
										break;
									}
									array_push($levelArray, $levelname["$k"]);
								}
								$levelstr = implode(",", $levelArray);
								$levelstr = DataUtil::is_empty($levelstr) ? "潜水,冒泡,吐槽,活跃,唠叨,传说" : $levelstr;
								$updateSql = new UpdateSql("web_group_info");
								$updateSql->setUpdateValue("adm_max", $adm_max);
								$updateSql->setUpdateValue("adm_num", $adm_num);
								$updateSql->setUpdateValue("count", $count);
								$updateSql->setUpdateValue("max_count", $max_count);
								$updateSql->setUpdateValue("level", $levelstr);
								$updateSql->setWhere("uin", $this->object['group_uin']);
								$updateSql->executeUpdateSql($this->db);
							}
							$result = $this->groupmemberinfoPer->replaceMe($this->object['group_uin'], $mems);
							if (!$result || $end + 1 >= $search_count) {
								break;
							}
							$start = $end + 1;
							$end = $start + 20;
						} else {
							break;
						}
					}
				}
				$tip = "";
				if ($result || $start > 0) {
					$upd_count = ($end == 20 || $end + 1 > $count) ? $count : $end + 1;
					$tip = "群成员更新成功，本次共更新了" . $upd_count . "个人的资料";
				} else {
					$tip = "群成员更新失败";
				}
				return ReplyUtil::getReply($tip);
			} elseif ($msg == "更新好友列表") {
				$result = false;
				$replace_values = array();
				$friendListArray = WqCommon::getFriendList($this->robot['bkn'], $this->cookie);
				if ($friendListArray && $friendListArray['ec'] == 0) {
					$replaceSql = "REPLACE INTO web_friend_info(uin, name, gtype, gname) VALUES";
					$friendResult = $friendListArray['result'];
	
					for ($k = 0; ;$k++) {
						if (!isset($friendResult["$k"])) {
							break;
						}
						$group = $friendResult["$k"];
						$groupName = "我的好友";
						if (isset($group['gname'])) {
							$groupName = $group['gname'];
						}
						if (isset($group['mems'])) {
							$friends = $group['mems'];
							foreach ($friends as $friend) {
								array_push($replace_values, "(" . $friend['uin'] . ",'" . $friend['name'] . "', " . $k . ", '" . $groupName . "')");
							}
						}
					}
					$replace_values_str = implode(",", $replace_values);
					if (!DataUtil::is_empty($replace_values_str)) {
						$result = $this->db->executeQuery($replaceSql . $replace_values_str);
					}
				}
				return ReplyUtil::getReply($result ? ("好友列表更新成功，本次共更新了" . count($replace_values) . "个人的资料") : "好友列表更新失败");
			} elseif (DataUtil::start_contain("添加管理员", $msg)) {
				$result = false;
				$profile = explode("添加管理员", $msg);
				$member_uin = trim($profile[1]);
				if (is_numeric($member_uin)) {
					$result = WqCommon::setGroupAdmin($this->object['group_uin'], $member_uin, 1, $this->robot['bkn'], $this->cookie);
				}
				return ReplyUtil::getReply($result ? "添加成功，{$member_uin}已经成为群管理员" : "添加失败");
			} elseif (DataUtil::start_contain("取消管理员", $msg)) {
				$result = false;
				$profile = explode("取消管理员", $msg);
				$member_uin = trim($profile[1]);
				if (is_numeric($member_uin)) {
					$result = WqCommon::setGroupAdmin($this->object['group_uin'], $member_uin, 0, $this->robot['bkn'], $this->cookie);
				}
				return ReplyUtil::getReply($result ? "取消成功，{$member_uin}已经不是群管理员啦" : "取消失败");
			} elseif (DataUtil::start_contain("禁言", $msg)) {
				$profile = explode("禁言", $msg);
				$profile = explode("|", $profile[1]);
				$member_uin = trim($profile[0]);
				$time_memo = trim($profile[1]);
				$result = $this->setMemberSpeech($this->object['group_uin'], $member_uin, $time_memo);
				return ReplyUtil::getReply($result ? "禁言{$member_uin}执行成功" : "禁言{$member_uin}执行失败");
			} elseif (DataUtil::start_contain("改名片", $msg)) {
				$profile = explode("改名片", $msg);
				$profile = explode("|", $profile[1]);
				if (count($profile) > 1) {
					$member_uin = trim($profile[0]);
					$card_name = trim($profile[1]);
					$result = $this->updateMemberCard($this->object['group_uin'], $member_uin, $card_name);
					return ReplyUtil::getReply($result ? "名片修改成功" : "名片修改失败");
				}
			} elseif (DataUtil::start_contain("踢人", $msg)) {
				$profile = explode("踢人", $msg);
				$member_uin = trim($profile[1]);
				$result = $this->removeMember($this->object['group_uin'], $member_uin);
				return ReplyUtil::getReply($result ? "{$member_uin}已被移出此群" : "{$member_uin}移出失败");
			} elseif (DataUtil::start_contain("加金币", $msg)) {
				$profile = explode("加金币", $msg);
				$profile = explode("|", $profile[1]);
				if (count($profile) > 1) {
					$member_uin = trim($profile[0]);
					$gold = trim($profile[1]);
					if (is_numeric($gold)) {
						$result = $this->groupmemberPer->updateGoldByManager($this->robot['id'], $this->object['group_uin'], $member_uin, $gold, true);
						return ReplyUtil::getReply($result ? "金币充值成功，请注意查收" : "金币充值失败");
					}
				}
			} elseif (DataUtil::start_contain("减金币", $msg)) {
				$profile = explode("减金币", $msg);
				$profile = explode("|", $profile[1]);
				if (count($profile) > 1) {
					$member_uin = trim($profile[0]);
					$gold = trim($profile[1]);
					if (is_numeric($gold)) {
						$result = $this->groupmemberPer->updateGoldByManager($this->robot['id'], $this->object['group_uin'], $member_uin, $gold, false);
						return ReplyUtil::getReply($result ? "金币没收成功" : "金币没收失败");
					}
				}
			} elseif ($msg == "重新运行") {
				die();
			}
		}
		if (DataUtil::is_equal("个人中心", $msg)) {
			$info = $this->getInfoForMsg();
			$nick_name = $this->getNameForMsg($info);
			$reply = $nick_name . "：" . CR;
			$reply .= "ＱＱ：" . $this->object['member_uin'] . CR;
			$reply .= "金币：" . $this->object['gold'] . CR;
			$reply .= "经验：" . $this->object['experience'];
			if ($info) {
				$reply .= CR . "头衔：" . $this->getLevelName($this->getGroupInfo(), $info['level']) . CR;
				$reply .= "积分：" . $info['point'] . CR;
				$reply .= "权限：" . ($info['role'] == 0 ? "群主" : ($info['role'] == 1 ? "管理员" : "群成员")) . CR;
				$reply .= "Ｑ龄：" . $info['qage'] . CR;
				$reply .= "性别：" . ($info['qsex'] == 0 ? "男" : ($info['qsex'] == 1 ? "女" : "未知")) . CR;
				$reply .= "入群时间：" . date('Y-m-d', $info['join_time']);
			}
			return ReplyUtil::getReply($reply);
		} elseif ($msg == "积分榜") {
			$pointArray = $this->groupmemberinfoPer->getMeByProfile($this->object['group_uin'], "point", "DESC", 10);
			if ($pointArray) {
				$reply = "==>积分排行榜" . CR;
				for ($i = 0; $i < count($pointArray); $i++) {
					$reply .= (FaceUtil::get(36) . ($i+1) . "、" . $this->getNameForMsg($pointArray[$i], $pointArray[$i]['member_uin']) . "（" . $pointArray[$i]['point'] . "）" . CR);
				}
				return ReplyUtil::getReply($this->getPublicReply($reply));
			}
			return ReplyUtil::getReply("积分排行失败");
		} elseif ($msg == "金币榜" || $msg == "土豪榜") {
			$goldArray = $this->groupmemberPer->getMeByProfile($this->robot['id'], $this->object['group_uin'], "gold", "DESC", 10);
			if ($goldArray) {
				$reply = "==>金币排行榜" . CR;
				for ($i = 0; $i < count($goldArray); $i++) {
					$reply .= (FaceUtil::get(36) . ($i+1) . "、" . $this->getNameForMsg($this->getInfoForMsg($goldArray[$i]['member_uin']), $goldArray[$i]['member_uin']) . "（" . $goldArray[$i]['gold'] . "）" . CR);
				}
				return ReplyUtil::getReply($this->getPublicReply($reply));
			}
			return ReplyUtil::getReply("金币排行失败");
		} elseif ($msg == "经验榜") {
			$goldArray = $this->groupmemberPer->getMeByProfile($this->robot['id'], $this->object['group_uin'], "experience", "DESC", 10);
			if ($goldArray) {
				$reply = "==>经验排行榜" . CR;
				for ($i = 0; $i < count($goldArray); $i++) {
					$reply .= (FaceUtil::get(36) . ($i+1) . "、" . $this->getNameForMsg($this->getInfoForMsg($goldArray[$i]['member_uin']), $goldArray[$i]['member_uin']) . "（" . $goldArray[$i]['experience'] . "）" . CR);
				}
				return ReplyUtil::getReply($this->getPublicReply($reply));
			}
			return ReplyUtil::getReply("经验排行失败");
		}
		return false;
	}

	public function getPublicReply($reply) {
		$public_reply = FaceUtil::get(29) . FaceUtil::get(29) . FaceUtil::get(29) . FaceUtil::get(29) . FaceUtil::get(29) . FaceUtil::get(29) . CR;
		$public_reply .= $reply;
		$public_reply .= FaceUtil::get(29) . FaceUtil::get(29) . FaceUtil::get(29) . FaceUtil::get(29) . FaceUtil::get(29) . FaceUtil::get(29);
		return $public_reply;
	}
} 
?>